<?php

namespace App\Http\Controllers\Admin;

use AlphaSnow\OSS\AppServer\Factory;
use App\Service\Common\UploadService;
use Carbon\Carbon;
use DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;
use Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CommonController extends AdminBaseController
{
    /**
     * OSS直传
     * @return JsonResponse|object
     */
    public function getOssUploadToken(Request $request) {
        $dir = 'admin/';
        if ($request->input('dir') && is_string($request->input('dir'))) {
            if (preg_match('/^(\w+\/)+?(\w+\/)*$/', $request->input('dir'))) {
                $dir.=$request->input('dir');
            }
        }

        $token = (new Factory([
            'access_key_id' => config('filesystems.disks.aliyun.access_id'),
            'access_key_secret' => config('filesystems.disks.aliyun.access_key'),
            'bucket' => config('filesystems.disks.aliyun.bucket'),
            'endpoint' => config('filesystems.disks.aliyun.endpoint'),
            'host' => config('filesystems.disks.aliyun.domain'),
            'callback_url' => '',
            'user_dir' => '/admin',
        ]))->makeToken();

        $token->policy()->setUserDir($dir)->setExpireTime(3600);
        return $this->Restful($token->response()->toArray());
    }


    /**
     * 上传文件到本地，适用于导入时上传word
     * @param Request $request
     * @return JsonResponse|object
     */
    public function uploadByImport(Request $request) {
        $file = $request->file('file');
        $prefix = request('prefix', '');
        if (!empty($prefix)) $prefix = '/'.$prefix;

        if (!$file) {
            throw ValidationException::withMessages(['message' => '请上传文件']);
        }

        // 保存文件
        $extension = $file->extension();
        $filName = Uuid::uuid4()->getHex().'.'.$extension;
        $path = $file->storeAs('uploads/import' . $prefix, $filName, 'local');

        return $this->restful([
            'name' => $filName,
            'relativePath' => 'app/'.$path,
            'fullPath' => storage_path($path)
        ]);
    }

    /**
     * 模板下载
     * @return BinaryFileResponse
     */
    public function downloadTemplate() {
        $path = '';
        switch (request('type')) {
            case 'paper':
                $path = public_path('template/PAPER.docx');
                break;
            case 'knowledge':
                $path = public_path('template/KNOWLEDGE.docx');
                break;
            case 'qhQuestion':
                $path = public_path('template/QH_QUESTION.docx');
                break;
        }

        if (empty($path)) {
            abort(404);
        }

        return Response::download($path);
    }

    public function getInitData() {
        $data = [
            'qh' => [
                'noticeType' => [
                    ['value' => 1, 'label' => '内部公告'],
                    ['value' => 2, 'label' => '考试动态'],
                    ['value' => 3, 'label' => '院校公告'],
                ],
                'urlType' => [
                    // ['value' => 0, 'label' => '不跳转'],
                    ['value' => '0', 'label' => '不跳转'],
                    // ['value' => 1, 'label' => '小程序页面'],
                    ['value' => '1', 'label' => '小程序页面'],
                    // ['value' => 2, 'label' => '小程序文章'],
                    ['value' => '2', 'label' => '小程序文章'],
                    // ['value' => 3, 'label' => '其他小程序'],
                    ['value' => '3', 'label' => '其他小程序'],
                    // ['value' => 4, 'label' => '公众号文章'],
                    ['value' => '4', 'label' => '公众号文章'],
                    // ['value' => 5, 'label' => '客服'],
                    ['value' => '5', 'label' => '客服'],
                    // ['value' => 6, 'label' => '拨打电话'],
                    ['value' => '6', 'label' => '拨打电话'],
                    // ['value' => 7, 'label' => '进入直播间'],
                    ['value' => '7', 'label' => '进入直播间'],
                    // ['value' => 8, 'label' => '跳转视频号'],
                    ['value' => '8', 'label' => '跳转视频号'],
                    // ['value' => 9, 'label' => '打开网页'],
                    ['value' => '9', 'label' => '打开网页'],
                ],
                'bannerUrlType' => [
                    // ['value' => 0, 'label' => '不跳转'],
                    ['value' => '0', 'label' => '不跳转'],
                    // ['value' => 1, 'label' => '小程序页面'],
                    ['value' => '1', 'label' => '小程序页面'],
                    // ['value' => 2, 'label' => '小程序文章'],
                    ['value' => '2', 'label' => '小程序文章'],
                    // ['value' => 3, 'label' => '其他小程序'],
                    ['value' => '3', 'label' => '其他小程序'],
                    // ['value' => 4, 'label' => '公众号文章'],
                    ['value' => '4', 'label' => '公众号文章'],
                    // ['value' => 5, 'label' => '客服'],
                    ['value' => '5', 'label' => '客服'],
                    // ['value' => 6, 'label' => '拨打电话'],
                    ['value' => '6', 'label' => '拨打电话'],
                    // ['value' => 7, 'label' => '进入直播间'],
                    ['value' => '7', 'label' => '进入直播间'],
                    // ['value' => 8, 'label' => '跳转视频号'],
                    ['value' => '8', 'label' => '跳转视频号'],
                    // ['value' => 9, 'label' => '打开网页'],
                    ['value' => '9', 'label' => '打开网页'],
                ],
                'articleType' => [
                    ['value' => 1, 'label' => '默认文章'],
                ],
                'cdkeyStatus' => [
                    ['value' => 1, 'label' => '未使用'],
                    ['value' => 2, 'label' => '已使用'],
                    ['value' => 3, 'label' => '已失效'],
                    ['value' => 4, 'label' => '已冻结'],
                ],
                'miniProgramComponent' => [
                    ['value' => 'swiper', 'label' => '轮播', 'image' => ''],
                    ['value' => 'big-swiper', 'label' => '大轮播', 'image' => ''],
                    ['value' => 'course-card', 'label' => '学科卡片', 'image' => ''],
                    ['value' => 'text-list', 'label' => '文字列表', 'image' => ''],
                    ['value' => 'grid-nav', 'label' => '宫格导航', 'image' => ''],
                    ['value' => 'small-title', 'label' => '小标题', 'image' => ''],
                    ['value' => 'user-data-statistics', 'label' => '考试统计卡片', 'image' => ''],
                    ['value' => 'user-card1', 'label' => '省份+错题统计卡片', 'image' => ''],
                    ['value' => 'notice', 'label' => '通知', 'image' => ''],
                    ['value' => 'big-title', 'label' => '居中大标题', 'image' => ''],
                    ['value' => 'empty', 'label' => '占位空组件', 'image' => ''],
                    //['value' => 'document', 'label' => '文档卡片', 'image' => ''],
                    //['value' => 'course-column', 'label' => '专栏', 'image' => ''],
                    //['value' => 'offiaccount', 'label' => '公众号', 'image' => ''],
                    ['value' => 'mini-article-card', 'label' => '小文章卡片', 'image' => ''],
                    ['value' => 'net-course-stat', 'label' => '网课统计卡片', 'image' => ''],
                    ['value' => 'learn-video-card', 'label' => '视频课程卡片', 'image' => ''],
                    ['value' => 'coupon', 'label' => '优惠券卡片', 'image' => ''],
                    //['value' => 'course-column-list', 'label' => '省份下专栏列表', 'image' => ''],
                    //['value' => 'learn-video-stat-card', 'label' => '视频课程统计卡片', 'image' => ''],
                    //['value' => 'course-column-stat-card', 'label' => '专栏统计卡片', 'image' => ''],
                ],
                'questionType' => array_values(collect(DB::table('qh_question_types')->pluck('name', 'id')->toArray())->map(function ($item, $key) {
                    return ['value' => $key, 'label' => $item];
                })->toArray()),
                'schoolArticleType' => [
                    ['value' => 1, 'label' => '招生简章'],
                    ['value' => 2, 'label' => '招生资讯'],
                ],
                'schoolType' => [
                    ['value' => '综合类', 'label' => '综合类'],
                    ['value' => '理工类', 'label' => '理工类'],
                    ['value' => '农林类', 'label' => '农林类'],
                    ['value' => '医药类', 'label' => '医药类'],
                    ['value' => '师范类', 'label' => '师范类'],
                    ['value' => '语言类', 'label' => '语言类'],
                    ['value' => '财经类', 'label' => '财经类'],
                    ['value' => '政法类', 'label' => '政法类'],
                    ['value' => '体育类', 'label' => '体育类'],
                    ['value' => '艺术类', 'label' => '艺术类'],
                    ['value' => '民族类', 'label' => '民族类'],
                    ['value' => '军事类', 'label' => '军事类'],
                    ['value' => '其他', 'label' => '其他'],
                ],
                'schoolLevel' => [
                    ['value' => '普通本科', 'label' => '普通本科'],
                    ['value' => '专科（高职）', 'label' => '专科（高职）'],
                ],
                'schoolBxType' => [
                    ['value' => '公办', 'label' => '公办'],
                    ['value' => '民办', 'label' => '民办'],
                    ['value' => '中外合作办学', 'label' => '中外合作办学'],
                    ['value' => '内地与港澳台地区合作办学', 'label' => '内地与港澳台地区合作办学'],
                ],
            ]
        ];

        return $this->restful($data);
    }

    public function getRegionLibrary() {
        //$list = \DB::table('area')->where()
    }

    public function getProvinceLibrary() {
        $list = DB::table('area')
            ->where('pid', 0)
            ->select('name as value', 'name as label')
            ->get();

        return $this->restful($list);
    }

    /**
     * 上次文件到ftp
     * @param Request $request
     * @param UploadService $service
     * @return JsonResponse|object
     * @throws ValidationException
     */
    public function uploadFile(Request $request, UploadService $service) {
        $file = $service->uploadToFtp(\request()->all());
        return $this->restful($file);
    }
}
